{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 文件I/O"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入：读取键盘输入\n",
      "你输入的内容是： 读取键盘输入\n"
     ]
    }
   ],
   "source": [
    "str = input('请输入：')\n",
    "\n",
    "print('你输入的内容是：',str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### open 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文件是： test1\n",
      "文件是否关闭： False\n",
      "文件是否关闭： True\n",
      "读取test2的内容是： python 璇诲彇鏂囦欢\n",
      "文件位置： 19\n"
     ]
    }
   ],
   "source": [
    "# 必须先用Python内置的open()函数打开一个文件，创建一个file对象，相关的方法才可以调用它进行读写。\n",
    "\n",
    "fo = open('test1','w')\n",
    "print('文件是：', fo.name)\n",
    "print('文件是否关闭：', fo.closed)\n",
    "fo.close()\n",
    "print('文件是否关闭：', fo.closed)\n",
    "fo = open('test1','w')\n",
    "fo.write('新添加的内容')\n",
    "fo.close()\n",
    "\n",
    "fo = open('test2','r+')\n",
    "text1 = fo.read()\n",
    "print('读取test2的内容是：',text1)\n",
    "\n",
    "file_position = fo.tell()\n",
    "\n",
    "print('文件位置：',file_position)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 重命名和删除文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "'''\n",
    "\n",
    "python的os模块提供了帮你执行文件处理操作的方法，比如重命名和删除文件。\n",
    "\n",
    "要使用这个模块，你必须先导入它，然后才可以调用相关的各种功能。\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "\n",
    "# 重命名文件test1到test_new1\n",
    "os.rename('test1','test_new1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
